Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 9 - Script Objects


About Script Objects

Script objects are user-defined objects that combine data (in the form of properties) and potential actions (in the form of handlers). Script object definitions are compound statements that can contain collections of properties, handlers, and other AppleScript statements.

Here is a simple script object definition:

script John
   property HowManyTimes : 0
   to sayHello to someone
      set HowManyTimes to HowManyTimes + 1
      return "Hello " & someone
   end sayHello
end script
It defines a script object that can handle the sayHello command. It assigns the script object to the variable John. The definition includes a handler for the sayHello command. It also includes a property, called HowManyTimes, that indicates how many times the sayHello command has been called.

A handler within a script object definition follows the same syntax rules as a subroutine definition. Unlike a subroutine definition, however, you can group a handler within a script object definition with properties whose values are related to the handler's actions.

After you define a script object, you initialize it by running the script that contains the script object definition. You can then use a Tell statement to
send commands to the script object. For example, the following statement sends the sayHello command the script object defined above.

tell John to sayHello to "Herb"
The result is "Hello Herb".

You can manipulate the properties of script objects in the same way you manipulate the properties of system and application objects. Use the Get command to get the value of a property and the Set or Copy command to change the value of a property.

The following statement uses a Get command to get the value of the HowManyTimes property of script object John.

get HowManyTimes of John
if the result > 10
   return "John, aren't you tired of saying hello?"end if

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996